/*Test Distrubition Server. Copyright (C) 2007-2008 Cameron de Witte. (cameron.dewitte@gmail.com)
* 
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
* License as published by the Free Software Foundation, either version 3 of the License, or any later version. 
* 
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* 
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public 
* License along with this program. If not, see http://www.gnu.org/licenses/.
*
* This file also partially written by Nigel Thorne (http://www.nigelthorne.com/blog/)
*/

using System;
using System.IO;
using System.Net.Sockets;

namespace TestDistrubitionServer
{
    public class SocketServer
    {
        private readonly IMainForm iFrm;
        private readonly Socket socketForClient;
        private string messageArguments = "";
        private int clientID;

        public SocketServer(Socket client, IMainForm iFrm)
        {
            socketForClient = client;
            
            this.iFrm = iFrm;
        }

        public void ServeSocket()
        {
            try
            {
                NetworkStream networkStream = new NetworkStream(socketForClient);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader reader = new StreamReader(networkStream);
                while (socketForClient.Connected)
                {
                    string message = reader.ReadLine();
                    string messageID = message.Split("|".ToCharArray()[0])[0];
                    string messageArgs = message.Split("|".ToCharArray(), 2)[1];
                    messageArguments = messageArgs;

                    switch (messageID)
                    {
                        case "1":
                            {
                                
                                clientID = iFrm.AddClient(messageArgs, "TEST");
                                iFrm.AddLine("Client " + messageArgs + " has connected.");
                                streamWriter.WriteLine("1|" + clientID);
                                streamWriter.Flush();
                                break;
                            }
                        case "2":
                            {
                                //Disconnect them.
                                socketForClient.Close();
                                iFrm.RemoveClient(Convert.ToInt32(messageArgs));
                                iFrm.AddLine("Client " + messageArgs + " has disconnected.");
                                break;
                            }
                    }

                    //streamWriter.WriteLine("Test Send Data");
                    //streamWriter.Flush();
                }
            }
            catch (IOException ex)
            {
                if (ex.Message.StartsWith("Unable to write data") || ex.Message.StartsWith("Unable to read data"))
                {
                    iFrm.AddLine("A connection to a client has been lost.");
                    iFrm.RemoveClient(clientID);
                }
                else
                {
                    iFrm.AddLine("ERROR: " + ex.Message);
                }
            }
        }
    }
}